﻿using ArbabTravelsERP.DAL;
using ArbabTravelsERP.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ArbabTravelsERP.Controllers
{
    public class VisaController : Controller
    {
        DAL.Master dal = new Master();
        DataUtility du = new DataUtility();
        All_Master master = new All_Master();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CreateVisa()
        {
            
            ViewData["GetSource"] = master.GetSource();
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateVisa( VisaModel visa)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(visa.FILE_PATH))
                {
                    var path = string.Empty;
                    var fileName = Convert.ToString(visa.REGISTRATION_NO) + "_" + Path.GetFileName(visa.FILE_PATH.ToString());
                    FileInfo file = new FileInfo(Server.MapPath("~/VisaUploadedFiles/" + Convert.ToString(visa.REGISTRATION_NO) + "_" + Convert.ToString(fileName)));

                    if (file.Exists)
                    {
                        file.Delete();
                    }

                    FileInfo file1 = new FileInfo(visa.FILE_PATH);
                    path = Path.Combine(Server.MapPath("~/VisaUploadedFiles/"), Convert.ToString(visa.REGISTRATION_NO) + "_" + Convert.ToString(fileName));
                    file1.CopyTo(path);

                }

                VisaDAL visaDAL = new VisaDAL();
                DataTable dt = null;
                dt = visaDAL.InsertVisaDetails(visa);

                if (dt.Rows[0]["Error"].ToString() == "Successfull")
                {
                    DataSet ds = new DataSet();
                    TempData["Success"] = "Success! Well done its submitted.";

                    return RedirectToAction("CreateVisa");
                }
            }
            ViewData["GetSource"] = master.GetSource();
            
            return View(visa);
        }

        public DataTable GetClientDetails()
        {
            SqlParameter[] param = new SqlParameter[1];
            SqlParameter CONDITIONAL_OPERATOR = new SqlParameter("@CONDITIONAL_OPERATOR", "GETCLIENT");

            param[0] = CONDITIONAL_OPERATOR;
            return du.ExecuteDataTable("StoredProcedure", "PROC_VISA_MASTER", param);
        }


	}
}